package com.abe.libfile;


import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.support.annotation.IntDef;

import java.io.File;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;

public class AbeFileManager {

    public interface OnFileChooseListener {
        void onFilePath(String path);
    }

    private static OnFileChooseListener chooseListener;
    private static Reference<AbeFileManager> fileManager;
    private static Reference<Activity> activityReference;
    private int currentType;

    private static final int REQUEST_CHOOSE_FILE = 0x101;

    public static final int TYPE_FILE_SD = 0x001;
    public static final int TYPE_FILE_ALL = 0x002;
    public static final int TYPE_FILE_IMAGE = 0x003;
    public static final int TYPE_FILE_VIDEO = 0x004;
    public static final int TYPE_FILE_AUDIO = 0x005;
    public static final int TYPE_FILE_IMAGE_AND_VIDEO = 0x006;

    @IntDef({TYPE_FILE_SD, TYPE_FILE_ALL, TYPE_FILE_IMAGE, TYPE_FILE_VIDEO, TYPE_FILE_AUDIO, TYPE_FILE_IMAGE_AND_VIDEO})
    @Retention(RetentionPolicy.SOURCE)
    public @interface FileType {

    }

    public static AbeFileManager with(Activity activity) {
        if (fileManager == null || fileManager.get() == null) {
            fileManager = new WeakReference<>(new AbeFileManager());
        }
        if (activityReference == null) {
            activityReference = new WeakReference<>(activity);
        }
        return fileManager.get();
    }

    public AbeFileManager setType(@FileType int type) {
        currentType = type;
        return fileManager.get();
    }

    public AbeFileManager setListener(OnFileChooseListener tempListener) {
        chooseListener = tempListener;
        return fileManager.get();
    }

    public void start() {
        //选择文件【调用系统的文件管理】
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        switch (currentType) {
            case TYPE_FILE_SD: {
                File sdCardDir = Environment.getExternalStorageDirectory();
                intent.setDataAndType(Uri.fromFile(sdCardDir), "*/*");
            }
            break;
            case TYPE_FILE_ALL: {
                intent.setType("*/*");//无类型限制
            }
            break;
            case TYPE_FILE_IMAGE: {
                intent.setType("image/*");//选择图片
            }
            break;
            case TYPE_FILE_VIDEO: {
                intent.setType("video/*");//选择视频 （mp4 3gp 是android支持的视频格式）
            }
            break;
            case TYPE_FILE_AUDIO: {
                intent.setType("audio/*");//选择音频
            }
            break;
            case TYPE_FILE_IMAGE_AND_VIDEO: {
                intent.setType("video/*;image/*");//同时选择视频和图片
            }
            break;
        }
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        activityReference.get().startActivityForResult(intent, REQUEST_CHOOSE_FILE);
    }

    public void destroy() {
        if (chooseListener != null) {
            chooseListener = null;
        }
        if (activityReference != null) {
            activityReference.clear();
            activityReference = null;
        }
        if (fileManager != null) {
            fileManager.clear();
            fileManager = null;
        }
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK && fileManager != null && activityReference != null) {
            switch (requestCode) {
                case REQUEST_CHOOSE_FILE: {
                    if (data != null) {
                        Uri uri = data.getData();
                        String path = FileChooseUtil.getInstance(activityReference.get()).getChooseFileResultPath(uri);
                        w(path);
                        if (chooseListener != null) {
                            chooseListener.onFilePath(path);
                        }
                    }
                }
                break;
            }
        }
    }

    private static void w(String msg) {
        System.out.println("AbeFileManager:" + msg);
    }
}
